import Vue from 'vue'
import App from './App.vue'
import router from './router'
import axios from 'axios'
import VueAxios from 'vue-axios'
import VueCookies from 'vue-cookies'

import 'lib-flexible/flexible'

import {
  Tabbar,
  TabbarItem,
  Search,
  GoodsAction, 
  GoodsActionIcon, 
  GoodsActionButton,
  NavBar,
  Toast,
  Loading,
  Stepper,
  SubmitBar,
  Icon,
  Checkbox,
  CheckboxGroup,
  Button,
  ActionSheet,
  Cell,
  Grid,
  GridItem,
  Swipe,
  SwipeItem,
  Form,
  Field,
  Overlay,
  Popup,
  SwipeCell,
  AddressList,
  AddressEdit,
  Area,
  Empty,
  Tag,
  Uploader,
  Tabs,
  Tab,
  PasswordInput,
  NumberKeyboard,
  Dialog,
  Image,
  Lazyload 
} from 'vant';

Vue
  .use(Tabbar)
  .use(TabbarItem)
  .use(Search)
  .use(GoodsAction)
  .use(GoodsActionButton)
  .use(GoodsActionIcon)
  .use(NavBar)
  .use(Toast)
  .use(Loading)
  .use(Stepper)
  .use(SubmitBar)
  .use(Icon)
  .use(Checkbox)
  .use(CheckboxGroup)
  .use(Button)
  .use(ActionSheet)
  .use(Cell)
  .use(Grid)
  .use(GridItem)
  .use(Swipe)
  .use(SwipeItem)
  .use(Form)
  .use(Field)
  .use(Overlay)
  .use(Popup)
  .use(SwipeCell)
  .use(AddressList)
  .use(AddressEdit)
  .use(Area)
  .use(Empty)
  .use(Tag)
  .use(Uploader)
  .use(Tabs)
  .use(Tab)
  .use(PasswordInput)
  .use(NumberKeyboard)
  .use(Dialog)
  .use(Image)
  .use(Lazyload)


Vue.use(VueAxios,axios)
Vue.use(VueCookies)
  
Vue.config.productionTip = false
// 全局注册组件

// 配置请求基础路径
axios.defaults.baseURL='http://www.kangliuyong.com:10002';

// 添加请求拦截器
axios.interceptors.request.use(function (config) {

  let appkey = 'U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA=';

  if (config.method === 'get') {
    config.params = config.params || {};
    config.params.appkey = appkey;
  }else if(config.method === 'post'){
    config.data - config.data || {};
    let paramsString = '';
    for (let key in config.data){
      paramsString += `${key}=${config.data[key]}&`
    }
    paramsString += `appkey=${appkey}`

    config.data = paramsString
  }
  // 在发送请求之前做些什么
  Toast.loading({
    message:'加载中。。。',
    forbidClick:true,
    duration:0
  });
  return config;
}, function (error) {
  // 对请求错误做些什么
  Toast.clear();
  return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  Toast.clear();
  return response;
}, function (error) {
  // 对响应错误做点什么
  Toast.clear();
  return Promise.reject(error);
});

// 对日期进行操作
Vue.filter('formatDate', (value, format) => {
  //value: 需要进行格式化的数据
  //format: 日期的格式 yyyy符号MM符号dd hh符号mm符号ss

  // 

  let currentDate = new Date(value);

  let year = currentDate.getFullYear();

  if (/(y+)/.test(format)) {
    //获取组匹配的内容
    let content = RegExp.$1;
    // 
    year = year.toString().slice(4 - content.length);
    format = format.replace(content, year);
  }

  // 

  let dateObject = {
    M: currentDate.getMonth() + 1,
    d: currentDate.getDate(),
    h: currentDate.getHours(),
    m: currentDate.getMinutes(),
    s: currentDate.getSeconds()
  };

  //进行替换format
  for (let key in dateObject) {
    //根据key生成动态正则表达式
    let reg = new RegExp(`(${key}+)`);
    // 

    if (reg.test(format)) {
      //获取组的匹配内容
      let c = RegExp.$1;
      // 
      let v = dateObject[key] >= 10 ? dateObject[key] : c.length === 2 ? '0' + dateObject[key] : dateObject[key];
      format = format.replace(c, v);
      // 
    }
  }

  


  return format;
})

new Vue({
  data: {
    //购物袋数量(公共状态)
    publicShopbagCount: ''
  },
  router,
  render: h => h(App)
}).$mount('#app')
